﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public interface IEvnetInfo
{
}
public class EventInfo : IEvnetInfo
{
    public object Sender;
    public Action EventAction;
}
public class EventInfo<F> : IEvnetInfo
{
    public object Sender;
    public Action<F> EventAction;
}
public class EventInfo<F, S> : IEvnetInfo
{
    public object Sender;
    public Action<F, S> EventAction;
}
public class EventInfo<F, S, T> : IEvnetInfo
{
    public object Sender;
    public Action<F, S, T> EventAction;
}

public class EventCenter
{
    private readonly static Dictionary<string, IEvnetInfo> mEventMessages = new Dictionary<string, IEvnetInfo>();

    public static void AddEventListener(string name, Action eventAction)
    {
        if (mEventMessages.ContainsKey(name))
        {
            (mEventMessages[name] as EventInfo).EventAction += eventAction;
        }
        else
        {
            mEventMessages.Add(name, new EventInfo() { EventAction = eventAction });
        }
    }

    public static void AddEventListener<F>(string name, Action<F> eventAction)
    {
        if (mEventMessages.ContainsKey(name))
        {
            (mEventMessages[name] as EventInfo<F>).EventAction += eventAction;
        }
        else
        {
            mEventMessages.Add(name, new EventInfo<F>() { EventAction = eventAction });
        }
    }

    public static void AddEventListener<F, S>(string name, Action<F, S> eventAction)
    {
        if (mEventMessages.ContainsKey(name))
        {
            (mEventMessages[name] as EventInfo<F, S>).EventAction += eventAction;
        }
        else
        {
            mEventMessages.Add(name, new EventInfo<F, S>() { EventAction = eventAction });
        }
    }
    public static void AddEventListener<F, S, T>(string name, object sender, Action<F, S, T> eventAction)
    {
        if (mEventMessages.ContainsKey(name))
        {
            (mEventMessages[name] as EventInfo<F, S, T>).EventAction += eventAction;
        }
        else
        {
            mEventMessages.Add(name, new EventInfo<F, S, T>() { EventAction = eventAction, Sender = sender });
        }
    }

    public static void RemoveEventListener(string name, Action eventAction)
    {
        if (mEventMessages.ContainsKey(name))
        {
            (mEventMessages[name] as EventInfo).EventAction -= eventAction;
        }
    }
    public static void RemoveEventListener<F>(string name, Action<F> eventAction)
    {
        if (mEventMessages.ContainsKey(name))
        {
            (mEventMessages[name] as EventInfo<F>).EventAction -= eventAction;
        }
    }
    public static void RemoveEventListener<F, S>(string name, Action<F, S> eventAction)
    {
        if (mEventMessages.ContainsKey(name))
        {
            (mEventMessages[name] as EventInfo<F, S>).EventAction -= eventAction;
        }
    }
    public static void RemoveEventListener<F, S, T>(string name, Action<F, S, T> eventAction)
    {
        if (mEventMessages.ContainsKey(name))
        {
            (mEventMessages[name] as EventInfo<F, S, T>).EventAction -= eventAction;
        }
    }

    public static void TriggerEvent(string name)
    {
        if (mEventMessages.ContainsKey(name))
        {
            (mEventMessages[name] as EventInfo).EventAction?.Invoke();
        }
    }
    public static void TriggerEvent<F>(string name, F arg)
    {
        if (mEventMessages.ContainsKey(name))
        {
            (mEventMessages[name] as EventInfo<F>).EventAction?.Invoke(arg);
        }
    }
    public static void TriggerEvent<F, S>(string name, F fArg, S sArg)
    {
        if (mEventMessages.ContainsKey(name))
        {
            (mEventMessages[name] as EventInfo<F, S>).EventAction?.Invoke(fArg, sArg);
        }
    }
    public static void TriggerEvent<F, S, T>(string name, F fArg, S sArg, T tArg)
    {
        if (mEventMessages.ContainsKey(name))
        {
            (mEventMessages[name] as EventInfo<F, S, T>).EventAction?.Invoke(fArg, sArg, tArg);
        }
    }
    public static void Clear()
    {
        mEventMessages.Clear();
}
}

